<template>
	<view>
		<block>
			<form @submit="subform">
				<view class="form-box">
					<view class="form-item flex-col" style="border-bottom:0">
						<view class="f1">导入订单<text style="color:red"> *</text></view>
					
						<view class="example-body">
							<uni-file-picker v-model="imageValue" limit="1" file-extname="xls,xlsx"
								file-mediatype="all" title="选择excel文件" @select="select" @progress="progress"
								@success="success" @fail="fail"></uni-file-picker>
						</view>
						<view class="f1" @click="fileDownload">模板下载</view>		
							<!-- <a class="f1" style="text-decoration: none;" href="https://dzp.yunmios.com/static/template/模板文件2.xlsx">模板下载</a> -->
					</view>
				</view>
				
				<button class="savebtn"
					:style="'background:linear-gradient(90deg,'+t('color1')+' 0%,rgba('+t('color1')+',0.8) 100%)'"
					form-type="submit">提交</button>
				<view style="height:50rpx"></view>
			</form>



		</block>

	</view>
</template>

<script>
import loginVue from '../index/login.vue';

	var app = getApp();

	export default {
		data() {
			return {
				imageValue:[]
			};
		},

		onLoad: function(opt) {
			this.opt = app.getopts(opt);
			this.testLogin();
		},
		methods: {
			testLogin(){
				app.get('ApiOrder/import1',{test:1},(resp)=>{
					console.log(resp);
				});
			},
			// 获取上传状态
			select(e) {
				let that=this;
				app.showLoading('上传中');
				uni.uploadFile({
					url: app.globalData.baseurl + 'ApiOrder/import1/aid/' + app
						.globalData.aid + '/platform/' + app.globalData.platform +
						'/session_id/' +
						app.globalData.session_id,
					filePath: e.tempFilePaths[0],
					name: 'file',
					success: function(res) {
						app.showLoading(false);
						if (typeof res.data == 'string') {
							//兼容微信小程序
							var data = JSON.parse(res.data);
						} else {
							//兼容百度小程序
							var data = res.data;
						}
						
						if(data.status == -10){
							app.alert(data.msg,()=>{
								app.goto('/pages/index/login', 'redirect');
							});
							
						}else if (data.status == 1) {
							// localStorage.setItem('tmp_oids',data.msg.join(','))
							uni.setStorageSync('tmp_oids',data.msg.join(','));
							app.alert('导入成功，请点击[确定]跳转批量支付中心',()=>{
								app.goto('/pages/my/payorder', 'redirect');
							});
						} else {
							app.alert(data.msg)
						}
					},
					fail: function(res) {
						app.showLoading(false);
						app.alert(res.errMsg);
					}
				});

			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},

			// 上传成功
			success(e) {
				console.log('上传成功')
			},

			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},

			subform: function(e) {
				var that = this;
				if (!that.imageValue[0]) {
					app.error('请先上传excel文件');
					return false;
				}
				
				app.post('ApiAdminOrder/drorder', {
					plfh_file: that.imageValue[0]
				}, function(res) {
					if (res.status == 0) {
						app.error(res.msg);
					} else {
						that.imageValue=[];
						app.success(res.msg);
						setTimeout(function() {
							app.goto('/admin/index/index', 'redirect');
						}, 1000);
					}
				});
			},

			fileDownload(){
				//这里填写接口地址
				let url = 'https://dzp.yunmios.com/static/template/模板文件2.xlsx';
								
				uni.downloadFile({  //调用官方提供的uni.downloadFile
					url: url,//将刚刚提前准备好的url传过来
					header: {
						//如果需要填写请求头信息则在此处填写
					},
					success: function (res) {
						if (res.statusCode === 200) {
							console.log('下载成功：', res.tempFilePath);
							//下载成功，并把下载好的文件保存在临时区域中
							uni.saveFile({//使用该功能，以将临时文件保存在手机中可长久存储的区域
								tempFilePath: res.tempFilePath,
								success: (saveResult) => {
									// 在保存文件成功后，使用 uni.openDocument 方法打开文件
									console.log("文件保存成功:",saveResult)
									uni.openDocument({
										filePath: saveResult.savedFilePath,
										fileType: fileType,  //这里把预先限定好的文件类型传来
										success: function() {
										console.log("文件打开成功!");
										//至此，手机会从app中跳转到文件预览界面
										//之后可从预览界面返回app中
										},
										fail: function(err) {
											console.log('打开失败', err);
										},
									});
								},
								fail: function(err) {
									console.log('保存文件失败', err);
								},
							});
							
						}
					},
					fail: function (err) {
						console.log('下载失败：', err);
					}
				});
			}

		}
	};
</script>
<style>
	radio {
		transform: scale(0.6);
	}

	checkbox {
		transform: scale(0.6);
	}

	.form-box {
		padding: 2rpx 24rpx 0 24rpx;
		background: #fff;
		margin: 24rpx;
		border-radius: 10rpx
	}

	.form-item {
		line-height: 100rpx;
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #eee
	}

	.form-item .f1 {
		color: #222;
		width: 200rpx;
		flex-shrink: 0
	}

	.form-item .f2 {
		display: flex;
		align-items: center
	}

	.form-box .form-item:last-child {
		border: none
	}

	.form-box .flex-col {
		padding-bottom: 20rpx
	}

	.form-item input {
		width: 100%;
		border: none;
		color: #111;
		font-size: 28rpx;
		text-align: right
	}

	.form-item textarea {
		width: 100%;
		min-height: 200rpx;
		padding: 20rpx 0;
		border: none;
	}

	.form-item .upload_pic {
		margin: 50rpx 0;
		background: #F3F3F3;
		width: 90rpx;
		height: 90rpx;
		text-align: center
	}

	.form-item .upload_pic image {
		width: 32rpx;
		height: 32rpx;
	}

	.savebtn {
		width: 90%;
		height: 96rpx;
		line-height: 96rpx;
		text-align: center;
		border-radius: 48rpx;
		color: #fff;
		font-weight: bold;
		margin: 0 5%;
		margin-top: 60rpx;
		border: none;
	}

	.ggtitle {
		height: 60rpx;
		line-height: 60rpx;
		color: #111;
		font-weight: bold;
		font-size: 26rpx;
		display: flex;
		border-bottom: 1px solid #f4f4f4
	}

	.ggtitle .t1 {
		width: 200rpx;
	}

	.ggcontent {
		line-height: 60rpx;
		margin-top: 10rpx;
		color: #111;
		font-size: 26rpx;
		display: flex
	}

	.ggcontent .t1 {
		width: 200rpx;
		display: flex;
		align-items: center;
		flex-shrink: 0
	}

	.ggcontent .t1 .edit {
		width: 40rpx;
		height: 40rpx
	}

	.ggcontent .t2 {
		display: flex;
		flex-wrap: wrap;
		align-items: center
	}

	.ggcontent .ggname {
		background: #f55;
		color: #fff;
		height: 40rpx;
		line-height: 40rpx;
		padding: 0 20rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
		margin-bottom: 10rpx;
		font-size: 24rpx;
		position: relative
	}

	.ggcontent .ggname .close {
		position: absolute;
		top: -14rpx;
		right: -14rpx;
		background: #fff;
		height: 28rpx;
		width: 28rpx;
		border-radius: 14rpx
	}

	.ggcontent .ggnameadd {
		background: #ccc;
		font-size: 36rpx;
		color: #fff;
		height: 40rpx;
		line-height: 40rpx;
		padding: 0 20rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
		margin-left: 10rpx;
		position: relative
	}

	.ggcontent .ggadd {
		font-size: 26rpx;
		color: #558
	}

	.ggbox {
		line-height: 50rpx;
	}


	.layui-imgbox {
		margin-right: 16rpx;
		margin-bottom: 10rpx;
		font-size: 24rpx;
		position: relative;
	}

	.layui-imgbox-img {
		display: block;
		width: 200rpx;
		height: 200rpx;
		padding: 2px;
		border: #d3d3d3 1px solid;
		background-color: #f6f6f6;
		overflow: hidden
	}

	.layui-imgbox-img>image {
		max-width: 100%;
	}

	.layui-imgbox-repeat {
		position: absolute;
		display: block;
		width: 32rpx;
		height: 32rpx;
		line-height: 28rpx;
		right: 2px;
		bottom: 2px;
		color: #999;
		font-size: 30rpx;
		background: #fff
	}

	.uploadbtn {
		position: relative;
		height: 200rpx;
		width: 200rpx
	}


	.clist-item {
		display: flex;
		border-bottom: 1px solid #f5f5f5;
		padding: 20rpx 30rpx;
	}

	.radio {
		flex-shrink: 0;
		width: 32rpx;
		height: 32rpx;
		background: #FFFFFF;
		border: 2rpx solid #BFBFBF;
		border-radius: 50%;
		margin-right: 30rpx
	}

	.radio .radio-img {
		width: 100%;
		height: 100%;
		display: block
	}

	.freightitem {
		width: 100%;
		height: 60rpx;
		display: flex;
		align-items: center;
		margin-left: 40rpx
	}

	.freightitem .f1 {
		color: #666;
		flex: 1
	}

	.detailop {
		display: flex;
		line-height: 60rpx
	}

	.detailop .btn {
		border: 1px solid #ccc;
		margin-right: 10rpx;
		padding: 0 16rpx;
		color: #222;
		border-radius: 10rpx
	}

	.detaildp {
		position: relative;
		line-height: 50rpx
	}

	.detaildp .op {
		width: 100%;
		display: flex;
		justify-content: flex-end;
		font-size: 24rpx;
		height: 60rpx;
		line-height: 60rpx;
		margin-top: 10rpx
	}

	.detaildp .op .btn {
		background: rgba(0, 0, 0, 0.4);
		margin-right: 10rpx;
		padding: 0 10rpx;
		color: #fff
	}

	.detaildp .detailbox {
		border: 2px dashed #00a0e9
	}

	.uni-popup-dialog {
		width: 300px;
		border-radius: 5px;
		background-color: #fff;
	}

	.uni-dialog-title {
		display: flex;
		flex-direction: row;
		justify-content: center;
		padding-top: 15px;
		padding-bottom: 5px;
	}

	.uni-dialog-title-text {
		font-size: 16px;
		font-weight: 500;
	}

	.uni-dialog-content {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 5px 15px 15px 15px;
	}

	.uni-dialog-content-text {
		font-size: 14px;
		color: #6e6e6e;
	}

	.uni-dialog-button-group {
		display: flex;
		flex-direction: row;
		border-top-color: #f5f5f5;
		border-top-style: solid;
		border-top-width: 1px;
	}

	.uni-dialog-button {
		display: flex;
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 45px;
	}

	.uni-border-left {
		border-left-color: #f0f0f0;
		border-left-style: solid;
		border-left-width: 1px;
	}

	.uni-dialog-button-text {
		font-size: 14px;
	}

	.uni-button-color {
		color: #007aff;
	}

	.stockwarning {
		position: absolute;
		right: 0rpx;
		bottom: 0;
		display: flex;
		align-items: center;
		font-size: 24rpx;
		color: red;
	}

	.stockwarning image {
		margin-right: 10rpx
	}
</style>